import Pagination from '@/components/Pagination'
import Cos from '@/myComponents/Cos'

export default {
  components: { Pagination, Cos },
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  inject: ['app'],
  provide() {
    return {
      app: this.app
    }
  },
  data() {
    return {
      list: [],
      dialog: {
        edit: {},
        title: '',
        type: ''
      },
      form: {},
      filter: {
        pageNumber: 20,
        pageCurrent: 1,
        pageAll: 1
      },
      visible: false
    }
  },
  methods: {
    getList(e) {
      this.app.loading = true
      let parameter = {}
      if (this.parameter)parameter = this.parameter
      if (this.getListBefore) this.getListBefore()
      this.reqList({
        ...this.filter,
        ...e,
        ...parameter
      }).then(response => {
        this.list = response.list ? response.list : []
        this.filter = response.filter ? response.filter : {}
        if (this.getListAfter) this.getListAfter(response)
        this.app.removeLoding()
      }).catch(err => {
        this.$message.error(err.msg ? err.msg : '获取数据失败')
        this.app.removeLoding()
      })
    },
    point: function(e) {
      var e = e || window.event
      var x = e.offsetLeft
      var y = e.offsetTop
      e = e.offsetParent
      while (e) {
        x += e.offsetLeft - e.scrollLeft
        y += e.offsetTop
        e = e.offsetParent
      }
      return {
        'x': x,
        'y': y
      }
    },
    handleWV(e) {
      const ss = e.split('.')
      const s = ss[ss.length - 1]
      if (s.toUpperCase() === 'PDF') {
        window.open(e, '_blank')
      } else if (s.toUpperCase() === 'XLSX' || s.toUpperCase() === 'XLS' || s.toUpperCase() === 'DOCX' || s.toUpperCase() === 'DOC') {
        window.open(
          'https://view.officeapps.live.com/op/view.aspx?src=' + e,
          '_blank'
        )
      } else if (s.toUpperCase() === 'PPT' || s.toUpperCase() === 'PPTX') {
        window.open(
          'http://view.officeapps.live.com/op/view.aspx?src=' + e,
          '_blank'
        )
      } else {
        window.open(e, '_blank')
      }
    },
    handleDelete(e) {
      this.$confirm('此操作将删除数据信息，数据将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.reqDel({
          ids: e['id']
        }).then(response => {
          this.getList()
          this.$message.success('删除成功')
        })
      })
    },
    handleDel(e) {
      this.$confirm('此操作将删除数据信息，数据将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.reqDel(e).then(response => {
          this.getList()
          this.$message.success('删除成功')
        })
      })
    },
    handleDialog(e) {
      this.dialog.title = e.dTitle ? e.dTitle : ''
      this.dialog.type = e.dType ? e.dType : ''
      this.dialog.edit = {}
      this.app.dialogParam(this.dialog.edit, e.dField ? e.dField : [], e)
      this.$refs[e.dRef ? e.dRef : 'dialog'].visible = true
    },
    handleEdit(e) {
      if (this.handleEditBefore) this.handleEditBefore(e)
      if (this.$refs['formDate']) {
        this.$refs['formDate'].validate(async(valid) => {
          if (valid) return this.toEdit(e)
        })
      } else {
        return this.toEdit(e)
      }
    },
    toEdit(e) {
      let data = {}
      e ? data = e : data = this.form
      if ((this.info && this.info.type) === 'add') {
        return this.toAdd(data)
      } else {
        return this.toUpd(data)
      }
    },
    toAdd(e) {
      return new Promise(async(resolve, reject) => {
        this.reqAdd(e).then(response => {
          if (this.getData) this.getData()
          this.handleCancel()
          this.$message.success('添加成功')
          resolve()
        }).catch(err => {
          this.$message.error(err && err.msg ? err.msg : err.toString())
          reject()
        })
      })
    },
    toUpd(e) {
      return new Promise(async(resolve, reject) => {
        this.reqUpd(e).then(response => {
          if (this.getData) this.getData()
          this.handleCancel()
          this.$message.success('编辑成功')
          resolve()
        }).catch(err => {
          this.$message.error(err && err.msg ? err.msg : err.toString())
          reject()
        })
      })
    },
    refreshParent() {
      if (this.getData) this.getData()
      this.handleCancel()
    },
    toFloat(v) {
      const f = parseFloat(v)
      if (isNaN(f)) return 0
      return f
    },
    out(e) {
      const blob = new Blob([e.data], { type: 'application/vnd.ms-excel;charset=utf-8' })
      const downloadElement = document.createElement('a')
      const href = window.URL.createObjectURL(blob)
      downloadElement.href = href
      downloadElement.download = decodeURI(e.headers['filename'])
      document.body.appendChild(downloadElement)
      downloadElement.click()
      document.body.removeChild(downloadElement)
      window.URL.revokeObjectURL(href)
    },
    fileChange() {
      this.$refs.upload.clearFiles()
    },
    files(e) {
      this.dialog.title = e.title
      this.dialog.edit = {}
      this.app.dialogParam(this.dialog.edit, ['e_id', 'type', 'db_type'],
        { 'e_id': e.e_id ? e.e_id : '',
          'type': e.type ? e.type : '',
          'db_type': e.db_type ? e.db_type : '' })
      this.$refs[e.dRef ? e.dRef : 'cos'].visible = true
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
